<template>
	<view class="pl24 pr24 pb24 pt24 ">
		<view class="bgfff pt24  bdra20  ">
			<u-cell-group :border="false">
				<u-cell @click="changeAvatar" :arrow="false" :borderTop="false" :label="'ID : '+userInfo.id"
					:title="userInfo.nickname">
					<u-avatar size="50" slot="right-icon" :src="userInfo.avatar"></u-avatar>
				</u-cell>
			</u-cell-group>
			<u-gap bgColor="#F8F8F8" height="16"></u-gap>
			<u-cell-group :border="false">
				<u-cell @click="modifyNickname" :arrow="false" :borderBottom="true" icon="grid" title="昵称"
					v-model="userInfo.nickname"></u-cell>
				<u-cell @click="modifyMobile" :borderBottom="true" icon="phone" title="手机号" :value="userInfo.phone||''">
					<u-button class="bind-mobile " :plain="true" size="small" slot="right-icon" type="info"
						v-if="userInfo.phone==null">立即绑定</u-button>
				</u-cell>
				<!-- 	<u-cell :borderBottom="true" icon="weixin-fill" title="微信号"
					:value="userInfo.mpwechatauth==null?'去绑定':'已绑定'"></u-cell> -->
				<u-cell @click="bindpwd" :borderBottom="true" icon="lock" title="设置密码" value=""></u-cell>
			</u-cell-group>
		</view>
		<u-modal cancelText="取消" confirmText="确认" @confirm="modalConfirm" @cancel="showMobile=false" :show="showMobile"
			:showCancelButton="true" :title="modalTitle">
			<view class="slot-content ">
				<u--form>
					<u-form-item :borderBottom="true" label="手机号: " labelWidth="70">
						<u--input border="none" placeholder="请输入手机号" v-model="mobile"></u--input>
					</u-form-item>
					<u-form-item :borderBottom="true" label="验证码:" labelWidth="70">
						<u--input border="none" placeholder="请输入验证码" v-model="code"></u--input>
						<view slot="right">
							<u-button @click="sendSMS" size type="warning" v-if="texttime==''">获取验证码</u-button>
							<u-button size type="warning" v-if="texttime!=''">{{texttime}}</u-button>
						</view>
					</u-form-item>
				</u--form>
			</view>
		</u-modal>
		<u-modal cancelText="取消" confirmText="确认" @cancel="showWechat=0" @confirm="identityConfirm" :show="showWechat"
			:showCancelButton="true" :title="modalTitle">
			<view class="slot-content ">
				<u--form>
					<u-form-item :borderBottom="true" :label="modalTitle+' : '" labelWidth="70">
						<u--input border="none" placeholder="请输入内容" v-model="modalContent"></u--input>
					</u-form-item>
				</u--form>
			</view>
		</u-modal>
		<u-modal cancelText="取消" confirmText="确认" :showCancelButton="true" title="注销账号" v-if="showcancel">
			<view class="cancel-account ">
				<text>注销后该微信将无法使用平台服务,不支持重复注册或者登录!</text>
			</view>
		</u-modal>

		<view class="log-out-bnt ">
			<u-button @click="log_out" :plain="true" shape="circle" type="default">退出登录</u-button>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				loading: !0,
				showMobile: false,
				showWechat: !1,
				code: "",
				mobile: "",
				time: 60,
				texttime: "",
				modalTitle: "hello",
				modalContent: "",
				type: 0,
				userInfo: {},
				showcancel: !1,
				isupdate: !0,
				push: !1,
				rand: "",
				showlanguage: !1,
				language: "",
				defaultIndex: [0],

			}
		},
		onLoad() {

		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			changeAvatar() {
				var t = this;
				uni.chooseImage({
					count: 1,
					sizeType: ["compressed"],
					success: function(n) {
						var a = n.tempFilePaths[0];
						uni.uploadFile({
							url: "https://kefu.juhuilm.cn/api/common/mpupload/image",
							filePath: a,
							name: "image",
							header: {
								Authorization: uni.getStorageSync('LOGIN_TOKEN_KEY')
							},
							formData: {
								type: "user"
							},
							success: function(e) {
								var n = {
									avatar: JSON.parse(e.data).data
								};
								t.$api["post"]("user/update-user", n).then(res => {
									if (res.code == 200) {
										t.getUserInfo();
									}
								});
								//console.log(n,"===n==");

							}
						})
					}
				})
			},
			modifyNickname() {
				this.showWechat = !0;
				this.modalTitle = "昵称";
				this.type = 2;
				this.modalContent = this.userInfo.nickname;
			},
			identityConfirm() {
				var n;
				var type = this.type;
				//wechat
				switch (type) {
					case 2:
						n = {
							nickname: this.modalContent
						}
						break;
					case 3:
						n = {
							pwd: this.modalContent
						}
						break;	
				}

				this.$api["post"]("user/update-user", n).then(res => {
					if (res.code == 200) {
						this.showWechat = 0;
						this.getUserInfo();
					}
				});
			},
			modalConfirm() {
				if (this.mobile == "") {
					this.$toast({
						title: "输入手机号"
					});
					return;
				}
				if (this.code == "") {
					this.$toast({
						title: "输入验证码"
					});
					return;
				}
				var key = uni.getStorageSync("code_key");
				if (key == "") {
					this.$toast({
						title: "获得验证码"
					});
					return;
				}
				this.$api["post"]("user/bind-phone", {
					phone: this.mobile,
					code: this.code,
					key: key
				}).then(res => {
					if (res.code == 200) {
						this.$toast({
							title: "绑定成功"
						});
						this.userInfo();
						this.showMobile = false;
					} else {
						this.$toast({
							title: res.message
						});
					}
				});

			},
			modifyMobile() {
				console.log(111);
				this.showMobile = true;
				this.modalTitle = "绑定手机号";
				this.modalContent = this.userInfo.mobile;
			},
			bindpwd(){
				this.showWechat = !0;
				this.modalTitle = "密码";
				this.type = 3;
				this.modalContent = ""
			},
			sendSMS() {
				var e = this,
					t = this;
				if (this.mobile == Number || 11 == this.mobile.length) {
					if ("" != this.texttime) {
						return;
					}
					if (this.mobile == this.userInfo.phone) {
						this.$toast({
							title: "手机号未改变"
						});
						return;
					}
					//check-phone
					this.$api["post"]("user/check-phone", {
						phone: this.mobile
					}).then(r => {
						if (r.code == 200) {
							this.$api["get"]("base/send-sms", {
								phone: this.mobile
							}).then(res => {
								//this.rand = res.data;
								if (res.code == 200) {
									uni.setStorageSync("code_key", res.data.key);
									this.$toast({
										title: "发送成功"
									});
									var n = this.time,
										a = setInterval((function() {
											0 == n ? (clearInterval(a), e.texttime = "") : (e
												.texttime = ""
												.concat(
													n)
												.concat(t.$t("秒重新获取")), n--)
										}), 1000)
								}
							});

						} else {
							this.$toast({
								title: "手机号已经被使用"
							})
						}
					});



				} else this.$toast({
					title: "请输入正确的手机号"
				})
			},
			getUserInfo() {
				this.$api['post']('user/refresh-user').then(res => {
					if (res.code == 200) {
						this.userInfo = res.data;
					}
				});
			},
			log_out() {
				uni.removeStorageSync('LOGIN_TOKEN_KEY');
				this.$go("index/index");
			}
		}
	}
</script>

<style scoped lang="scss">
	.slot-content {
		padding: 12upx
	}

	.slot-content button {
		font-size: 25upx;
		height: 50upx;
		width: 150upx
	}

	.slot-content .col-content {
		padding: 0 !important
	}

	.slot-content .item-content {
		margin-left: 20upx
	}

	.log-out-bnt {
		margin-top: 120upx;
		padding: 20upx
	}

	.log-out-bnt button {
		border: 2upx solid #2270fc;
		color: #2270fc
	}

	.bind-mobile {
		float: right;
		font-size: 26upx;
		height: 50upx;
		width: 160upx
	}

	.slot-content uni-button {
		width: 155upx !important
	}

	/deep/.u-modal__title {
		display: block !important;
	}
</style>